package com.test.seatastorage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.seatastorage.dao.StorageDao;
import com.test.seatastorage.entity.Storage;
import com.test.seatastorage.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class StorageServiceImpl extends ServiceImpl<StorageDao, Storage> implements StorageService {
    @Resource
    private StorageDao storageDao;

    public void decrease(Long productId, Integer count){
        log.info("------->seata-storage-samples中扣减库存开始-------<");

        Storage storage = storageDao.selectOne(new QueryWrapper<Storage>().lambda().eq(Storage::getProductId,productId));
        if (storage == null){
            return;
        }

        storage.setUsed(storage.getUsed() + count);
        storage.setResidue(storage.getResidue() + count);
        storageDao.updateById(storage);

        log.info("------->seata-storage-samples中扣减库存结束-------<");
    }
}
